﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Domain.Repositories;
using YAKEJI.Abp.Domain.Repositories;
using YAPACS.BusinessObjects.Exams;
using YAPACS.BusinessObjects.Managements.Exams;
using YAPACS.Organizations;
using YAPACS.RisServer.Interface.Imitate;
using YAPACS.RisServer.Services.Dtos;

namespace YAPACS.RisServer.Controllers;

[Authorize]
[ApiController]
public class ExamRemarkController(ICurrentUserOrganization currentUserOrganization,
    IRepository<ExamRemarkLevel, Guid> examRemarkLevelRepository,
    IRepository<Exam, Guid> examRepository,
    IRepository<ExamRemark, Guid> examRemarkRepository,
    IRepository<ApplicationUser, Guid> userRepository,
    IRepository<ExamRemarkTemplate, Guid> examRemarkTemplateRepository) : AbpControllerBase
{
    private readonly ICurrentUserOrganization _currentUserOrganization = currentUserOrganization;
    private readonly IRepository<ExamRemarkLevel, Guid> _examRemarkLevelRepository = examRemarkLevelRepository;
    private readonly IRepository<Exam, Guid> _examRepository = examRepository;
    private readonly IRepository<ExamRemark, Guid> _examRemarkRepository = examRemarkRepository;
    private readonly IRepository<ApplicationUser, Guid> _userRepository = userRepository;
    private readonly IRepository<ExamRemarkTemplate, Guid> _examRemarkTemplateRepository = examRemarkTemplateRepository;

    [HttpGet("api/get-exam-remark-options")]
    public async Task<IActionResult> GetRemarkLevelOptionsAsync()
    {
        List<ExamRemarkLevel> list = await _examRemarkLevelRepository.GetListAsync(e => e.IsActive && e.Department.Oid == _currentUserOrganization.DepartmentId.Value);
        var result = list.OrderBy(e=>e.Order).ThenBy(e=>e.CreationTime).Select(e => new
        {
            Label = e.Name,
            Value = e.Oid
        });
        return Ok(result);
    }
    [HttpGet("api/get-exam-remark-{levelId}")]
    public async Task<IActionResult> GetRemarkLevelAsync(Guid levelId)
    {
        var level = await _examRemarkLevelRepository.GetAsync(levelId);
        return Ok(new
        {
            Label = level.Name,
            Value = level.Oid
        });
    }

    [HttpPost("api/add-exam-remark")]
    public async Task<IActionResult> AddExamRemarkAsync([FromForm] ExamRemakDto examRemakDto)
    {
        Exam exam = await _examRepository.GetAsync(examRemakDto.Exam);
        ExamRemark examRemark = await _examRemarkRepository.NewEntityAsync();
        ExamRemarkLevel examRemarkLevel = await _examRemarkLevelRepository.GetAsync(examRemakDto.Level);
        examRemark.Content = examRemakDto.Content;
        examRemark.Level = examRemarkLevel;
        examRemark.User = await _userRepository.GetAsync(_currentUserOrganization.UserId);
        exam.ExamRemarks.Add(examRemark);
        return Ok(new { Success = true });
    }
    [HttpPost("api/delete-exam-remark")]
    public async Task<IActionResult> DeleteExamRemarkAsync([FromForm] ExamRemakDto examRemakDto)
    {
        await _examRemarkRepository.DeleteAsync(examRemakDto.Oid);
        return Ok(new { Success = true });
    }
    [HttpPost("api/update-exam-remark")]
    public async Task<IActionResult> UpdateExamRemarkAsync([FromForm] ExamRemakDto examRemakDto)
    {
        ExamRemark examRemark = await _examRemarkRepository.GetAsync(examRemakDto.Oid);
        ExamRemarkLevel examRemarkLevel = await _examRemarkLevelRepository.GetAsync(examRemakDto.Level);
        examRemark.Content = examRemakDto.Content;
        examRemark.Level = examRemarkLevel;
        return Ok(new { Success = true });
    }
    #region 获取科室检查备注模板
    [HttpGet]
    [Route("api/exam-detail/get-exam-remark-template")]
    public async Task<IActionResult> GetExamRemarkTemplate()
    {
        List<ExamRemarkTemplate> list = await _examRemarkTemplateRepository.GetListAsync(e => e.Department.Oid == _currentUserOrganization.DepartmentId);
        var result = list.Select(e => new { 
            e.Oid, 
            e.Name, 
            LevelName = e.Level.Name, 
            Level = e.Level.Oid, 
            e.Content });
        return Ok(result);
    }
    #endregion
    #region 添加检查备注
    [HttpGet]
    [Route("api/exam-detail/add-exam-remark")]
    public async Task<IActionResult> AddExamRemark(string content, Guid level, Guid examOid)
    {
        Exam exam = await _examRepository.GetAsync(examOid);
        ExamRemark examRemark = await _examRemarkRepository.NewEntityAsync();
        ExamRemarkLevel examRemarkLevel = await _examRemarkLevelRepository.GetAsync(level);
        examRemark.Content = content;
        examRemark.Level = examRemarkLevel;
        exam.ExamRemarks.Add(examRemark);
        var remarks = exam.ExamRemarks.ToList().Select(e => new { oid = e.Oid, level = e.Level.Oid.ToString(), levelName = e.Level.Name, content = e.Content });
        var data = new
        {
            remarks = remarks
        };
        return Ok(new AmisRes { Status = 0, Msg = "success", Data = data });
    }
    #endregion
    #region 更新注意事项
    [HttpGet]
    [Route("api/exam-detail/update-exam-remark")]
    public async Task<IActionResult> UpdateExamRemark(Guid examOid, Guid remarkOid, string content, Guid level)
    {
        AmisRes res = new AmisRes();
        try
        {
            Exam exam = await _examRepository.GetAsync(examOid);
            ExamRemark examRemark = await _examRemarkRepository.GetAsync(remarkOid);
            exam.ExamRemarks.Remove(examRemark);
            examRemark.Content = content;
            examRemark.Level = await _examRemarkLevelRepository.GetAsync(level);
            exam.ExamRemarks.Add(examRemark);
            await _examRepository.UpdateAsync(exam);
            var remarks = exam.ExamRemarks.ToList().Select(e => new { oid = e.Oid.ToString(), level = e.Level.Oid.ToString(), levelName = e.Level.Name, content = e.Content });
            var data = new
            {
                remarks = remarks
            };
            res.Data = data;
        }
        catch (Exception ex)
        {
            await UnitOfWorkManager.Current.RollbackAsync();
            res.Status = 1;
            res.Msg = ex.Message;
        }
        return Ok(res);
    }
    #endregion
    #region 删除检查备注
    [HttpGet]
    [Route("api/exam-detail/delete-exam-remark")]
    public async Task<IActionResult> DeleteExamRemark(Guid examOid, Guid remarkOid)
    {
        AmisRes res = new AmisRes();
        try
        {
            Exam exam = await _examRepository.GetAsync(examOid);
            ExamRemark examRemark = await _examRemarkRepository.GetAsync(remarkOid);
            exam.ExamRemarks.Remove(examRemark);
            var remarks = exam.ExamRemarks.ToList().Select(e => new { oid = e.Oid.ToString(), level = e.Level.Oid.ToString(), levelName = e.Level.Name, content = e.Content });
            var data = new
            {
                remarks = remarks
            };
            res.Data = data;
        }
        catch (Exception ex)
        {
            res.Status = 1;
            res.Msg = ex.Message;
        }
        return Ok(res);
    }
    #endregion
}
